package users;
import java.util.ArrayList;

import engine.Table;
import flex.messaging.MessageBroker;
import flex.messaging.messages.AsyncMessage;
import flex.messaging.util.UUIDUtils;

public class Playerlist {
	private static ArrayList<String> playerList;
	public Playerlist() {
		if(playerList == null) {
			playerList = new ArrayList<String>();
	}
	}
	public void addPlayer(String newPlayer) {
		if(playerList == null) {
			playerList = new ArrayList<String>();
		}
		if(playerList.size() < 4 && !playerList.contains(newPlayer)){
			playerList.add(newPlayer);
			sendPlayerlist();
		}
	}

	public void removePlayer(String player) {
		playerList.remove(player);
		sendPlayerlist();
	}
	
	public static void clear(){
		playerList.clear();
		sendPlayerlist();
	}
	public static ArrayList<String> getPlayerlist(){
		return playerList;
	}
	public static void sendPlayerlist() {
		try {
		Thread.sleep(200);
		} catch (InterruptedException e) {
		e.printStackTrace();
		}
		MessageBroker msgBroker = MessageBroker.getMessageBroker(null);
		AsyncMessage msg = new AsyncMessage();
		msg.setDestination("myPlayerlistDest");
		msg.setClientId(UUIDUtils.createUUID());
		msg.setMessageId(UUIDUtils.createUUID());
		msg.setTimestamp(System.currentTimeMillis());
		msg.setBody(playerList);
		msgBroker.routeMessageToService(msg, null);
	}
}